package com.tzstcl.sys.user.service.impl;

import com.tzstcl.base.service.impl.BaseServiceImpl;
import com.tzstcl.framework.constants.AppConstants;
import com.tzstcl.framework.shiro.ShiroUtils;
import com.tzstcl.sys.user.mapper.SysDeptMapper;
import com.tzstcl.sys.user.model.SysDept;
import com.tzstcl.sys.user.model.SysUser;
import com.tzstcl.sys.user.service.SysDeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 作者：guohy
 * 日期：2019年08月05日
 * 说明：用户部门ServiceImpl
 */
@Service
public class SysDeptServiceImpl extends BaseServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {

    /**
     * 批量增加用户部门
     *
     * @param sysDeptList
     * @return Integer 插入的记录数
     * @author guohy
     * @date 2019年08月05日
     */
    @Transactional(readOnly = false, rollbackFor = Exception.class)
    @Override
    public Integer insertBatch(List<SysDept> sysDeptList) {
        return this.mapper.insertBatch(sysDeptList);
    }

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Integer save(SysDept sysDept) {
        return insertOrUpdate(sysDept);
    }

    public List<SysDept> findListAll() {
        List<SysDept> list = mapper.findListAll();
        if (list.size() > 0) {
            return list;
        }
        return new ArrayList<>();
    }

    public List<SysDept> deptListForStatus() {
        List<SysDept> list = mapper.deptListForStatus();
        if (list.size() > 0) {
            return list;
        }
        return new ArrayList<>();
    }

    @Transactional(readOnly = false, rollbackFor = Exception.class)
    public Integer deletes(String[] ids) {
        return mapper.deletes(ids);
    }

    @Override
    public Integer add(SysDept sysDept) {
        SysDept dept = initSysDept(sysDept);
        return super.add(dept);
    }

    @Override
    public List<Map<String, String>> getDataForSelect() {
        return mapper.getDataForSelect();
    }


    public SysDept initSysDept(SysDept sysDept) {
        SysUser sysUser = ShiroUtils.getUser();
        if (null != sysDept) {
            //断言
            assert sysUser != null;
            String createBy = sysUser.getId() + "";
            String updateBy = sysUser.getId() + "";
            Date createTime = new Date();
            Date updateTime = new Date();
            String delFlag = AppConstants.TABLE_DELFLAG_VALID;
            sysDept.setCreateBy(createBy);
            sysDept.setUpdateBy(updateBy);
            sysDept.setCreateTime(createTime);
            sysDept.setUpdateTime(updateTime);
            sysDept.setDelFlag(delFlag);
        }
        assert sysDept != null;
        return sysDept;
    }

    @Override
    public List<SysDept> findListByDept(Long depdId) {
        return mapper.findListByDept(depdId);
    }
}